package com.dchh.android.suverynew.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.bean.Node;
import com.dhcc.android.base.common.bean.Task;
import com.dhcc.android.base.utils.Constants;

import java.util.List;
import java.util.Map;

/**
 * 图纸拍摄列表适配器
 *
 * @author zhoudan
 * @date 2016年2月22日
 */
public class TZPSListAdapter extends BaseAdapter {

    private Activity activity;
    private List<Task> taskList;
    private List<Map<String, String>> dataList;
    private Node node;

    private int type = 1;//1-任务包   2-村   3-样方  

    private OnOperateListener listener;

    private int count;

    public TZPSListAdapter(Activity activity, List<Task> taskList) {
        this.activity = activity;
        this.taskList = taskList;
        this.type = 1;
    }

    public TZPSListAdapter(Activity activity, List<Map<String, String>> dataList, Node node, int type) {
        this.activity = activity;
        this.dataList = dataList;
        this.node = node;
        this.type = type;
    }

    public void setOnOperateListener(OnOperateListener listener) {
        this.listener = listener;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public int getCount() {
        int n = 0;
        if (type == 1) {
            n = taskList.size();
        } else {
            n = dataList.size();
        }
        return n;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;
        if (row == null) {
            holder = new ViewHolder();

            row = LayoutInflater.from(activity).inflate(R.layout.paper_photo_add_list_item, null);
            holder.title = (TextView) row.findViewById(R.id.item_title);
            holder.take = (ImageView) row.findViewById(R.id.take_photo);
            holder.show = (ImageView) row.findViewById(R.id.show_photo);

            if (type == 3) {
                row.findViewById(R.id.note_icon).setVisibility(View.GONE);
            } else {
                row.findViewById(R.id.note_icon).setVisibility(View.VISIBLE);
            }

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        if (type == 1) {
            Task task = taskList.get(position);
            holder.title.setText(task.getTitle());

            if (count > 0) {//判断是否已经拍照
                holder.take.setVisibility(View.GONE);
                holder.show.setVisibility(View.VISIBLE);

                row.setBackgroundResource(R.drawable.tz_item_white_selector);
            } else {
                holder.take.setVisibility(View.VISIBLE);
                holder.show.setVisibility(View.GONE);

                row.setBackgroundResource(R.drawable.tz_item_gray_selector);
            }

        } else {
            Map<String, String> map = dataList.get(position);
            if (type == 2) {
                String v = map.get(node.getItem().getFeild());
                holder.title.setText(v);

            } else {
                String v = Constants.tzps_parent.get("CUNMC") + "-样方" + map.get(node.getItem().getFeild());
                holder.title.setText(v);
            }

            String sflag = map.get("flag");
            int iflag = 0;
            if (sflag != null) {
                iflag = Integer.valueOf(sflag);
            }
            if (iflag > 0) {//判断是否已经拍照
                holder.take.setVisibility(View.GONE);
                holder.show.setVisibility(View.VISIBLE);

                row.setBackgroundResource(R.drawable.tz_item_white_selector);
            } else {
                holder.take.setVisibility(View.VISIBLE);
                holder.show.setVisibility(View.GONE);

                row.setBackgroundResource(R.drawable.tz_item_gray_selector);
            }
        }

        holder.take.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnTake(position);
                }
            }
        });
        holder.show.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.OnShow(position);
                }
            }
        });

        return row;
    }


    public final class ViewHolder {
        public TextView title;
        public ImageView take;
        public ImageView show;
    }

    public interface OnOperateListener {
        public void OnTake(int position);

        public void OnShow(int position);
    }
}
